/**
 * 
 */
package cz.cuni.mff.abacs.burglar.visual;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import cz.cuni.mff.abacs.burglar.visual.play_state.GraphicPlayState;


/**
 * 
 * 
 * @author abacs
 *
 */
public class Main extends StateBasedGame {
	
	// constants:
	
	/** The string put into the title bar */
	protected static String TITLE = "Visual Burglar";
	
	/** The default width of the drawing area */
	protected static int DEFAULT_WIDTH  = 1200;
	/** The default height of the drawing area */
	protected static int DEFAULT_HEIGHT = 500;
	
	/** The string printed to the user if the input parameters are invalid */
	protected static String USAGE_STRING = 
		"options: \n" + 
		"\t[-h %d]\twidth\n" +
		"\t[-w %d]\theight\n" + 
		"[-r]\tshow frame rate\n" +
		"[-f]\tfull screen mode\n";
	
	/**  */
	public static final int MENU_STATE = 0;
    /**  */
	public static final int PLAY_STATE = 1;
	/**  */
	public static final int LEVEL_SELECT_STATE = 2;
	
    /** The width of the images in the resource directory */
	public static final int RESOURCE_BLOCK_SIZE = 32;
	
	/** '/' terminated path to the resources directory. */
	public static String RESOURCE_PATH = "res/";
	/** '/' terminated path to the button resources directory. */
	public static String BUTTON_PATH = RESOURCE_PATH + "img/buttons/";
	/** '/' terminated path to the planners directory. */
	public static String PLANNER_PATH = "planner/";
	
	public static String MAP_PATH = "maps/";
	
	
	/**  */
	public static boolean FLAG_ALL_KNOWING_AGENTS = false;
	
	public static boolean FLAG_LOOK_AROUND = false;
	
	public static boolean FLAG_SAVE_PLANNING_PROBLEMS = true;
	
	public static boolean FLAG_IN_GAME_LEVEL_DESIGN = true;
	
	// -------------------------------------------------------------------------
	// constructors:
	
	
	/**
	 * 
	 */
	public Main() throws SlickException {
		// set the title:
		super(TITLE);
		// set the states:
		this.addState(new MenuState(MENU_STATE));
		this.addState(new GraphicPlayState(PLAY_STATE));
		this.addState(new LevelSelectionState(LEVEL_SELECT_STATE));
		// start:
		this.enterState(MENU_STATE);
	}
	
	
	// -------------------------------------------------------------------------
	
	
	/** 
	 * Initialize the used states.
	 * 
	 * @param container
	 */
	@Override
	public void initStatesList(GameContainer container) throws SlickException {
		this.getState(MENU_STATE).init(container, this);
		this.getState(PLAY_STATE).init(container, this);
		this.getState(LEVEL_SELECT_STATE).init(container, this);
	}
	
	
	// -------------------------------------------------------------------------
	
	
	/** 
	 * 
	 * 
	 * arguments:
	 * "-w %d": width
	 * "-h %d": height
	 * "-r": show frame rate
	 * "-f": full screen mode
	 */
	public static void main(String[] args) {
		boolean showFrameRate = false;
		boolean fullScreen = false;
		int width  = Main.DEFAULT_WIDTH;
		int height = Main.DEFAULT_HEIGHT;
		
		// process the arguments:
		for(int i = 0; i < args.length; i++){
			// frame rate:
			if(args[i].equals("-r")){
				showFrameRate = true;
				continue;
			}
			// full screen:
			if(args[i].equals("-f")){
				fullScreen = true;
				continue;
			}
			// width:
			if(args[i].equals("-W")){
				if(args.length < i + 2){
					System.out.println(USAGE_STRING);
					System.exit(-1);
				}
				try{
					width = Integer.valueOf(args[++i]);
				}catch(NumberFormatException e){
					System.err.println(USAGE_STRING);
					System.exit(-1);
				}
				continue;
			}
			// height:
			if(args[i].equals("-h")){
				if(args.length < i + 2){
					System.out.println(USAGE_STRING);
					System.exit(-1);
				}
				try{
					height = Integer.valueOf(args[++i]);
				}catch(NumberFormatException e){
					System.err.println(USAGE_STRING);
					System.exit(-1);
				}
				continue;
			}
		}
		
		try{
			AppGameContainer app = new AppGameContainer(new Main());
			app.setDisplayMode(width, height, fullScreen);
			app.setShowFPS(showFrameRate);
			// render even when invisible
			app.setAlwaysRender(true);
			// synchronize the rendering cycles with the monitor's frame rate
			app.setVSync(true);
			// start the main loop:
			app.start();
		}catch(SlickException e){
			e.printStackTrace();
		}
	}

}
